<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
<style type="text/css">
	.center {
		text-align: center;
	}
	.modal-dialog {
		top: 5% ;
	}
	.tree-branch-name .fa-check {
		background-color: #F9A021;border-color: #F9A021;color: #FFF;width: 15px;height: 15px;line-height: 15px;font-size: 11px;text-align: center;border-radius: 3px;position: relative;bottom: 1px;margin-right: 5px
	}

</style>
<div class="page-header">
	<h1>
		教师管理
		<!-- 和教师权限不可添加 -->
		<?php if((! $rmxSchool['school_from'] || $rmxSchool['school_from'] == X_DATA_YINTONG) && $rmxUser['user_type'] != USER_TYPE_TEACHER): ?>
			<a href="<?=admin_url('school/teacher_edit')?>" class="btn btn-info a-custom">
				<i class="ace-icon fa fa-plus"></i> 新增教师
			</a>
			<span class="dropdown dropdown-custom">
				<div class="btn btn-info btn-dropdown"  data-toggle="dropdown">
					<i class="ace-icon fa fa-database"></i> 模板导入与导出&nbsp;&nbsp;<i class="ace-icon fa fa-caret-down"></i>
				</div>
				<ul class="dropdown-menu">
					<li>
						<a href="javascript:" id="import">
							<i class="ace-icon fa fa-upload"></i> 导入教师数据
						</a>
					</li>
					<li>
						<a href="https://resource.rongmixiao.com/2019/12/16/54096e594473955432df4739c0a261aa.xls" target="_blank">
							<i class="ace-icon fa fa-download"></i> 导出教师数据模板
						</a>
					</li>
				</ul>
			</span>
			</span>
		<?php endif ?>
		<a id="export_teacher" href="javascript:;" class="btn btn-info a-custom" target="_blank">
			<i class="ace-icon fa fa-download"></i> 导出教师数据
		</a>
	</h1>
</div>

<div class="col-xs-12 content-filter">
	<!-- 筛选 -->
	<div class="widget-box">
		<div class="widget-header">
			<h4 class="widget-title">内容筛选</h4>
			<div class="widget-toolbar">
				<a href="#" data-action="collapse">
					<i class="ace-icon fa fa-chevron-up"></i>
				</a>
				<a href="#" data-action="close">
					<i class="ace-icon fa fa-times"></i>
				</a>
			</div>
		</div>
		<div class="widget-body">
			<div class="widget-main" style="padding-top:15px;">
				<div class="form-inline keyword-search-div">
					<label class="keyword-search-label">名称&nbsp;&nbsp;&nbsp;&nbsp;</label>
					<div class="keyword-search-box">
						<div class="div-search">
							<input style="float:left;height:30px;width:180px;border:1px solid #669FC7;padding:0 30px 0 14px;border-radius:10px 0 0 10px !important;-moz-border-radius:10px 0 0 10px;" type="text" name="keyword" placeholder="搜索教师名称..." autocomplete="off">
							<span id="empty-search" style="position:relative;right:110px;cursor:pointer;display:none;">×</span>
							<button id="btn-search" class="btn-search btn-info" style="outline:none;float:left;height:30px;width:44px;border:1px solid #669FC7;border:0;border-radius:0 10px 10px 0;-moz-border-radius:0 10px 10px 0;margin-right:40px;">
								<i class="ace-icon fa fa-search nav-search-icon white"></i>
							</button>
							<!-- 教师权限不可编辑 -->
							<?php if($rmxUser['user_type'] != USER_TYPE_TEACHER): ?>
								<button type="button" class="btn btn-info btn-sm" style="height: 30px;position:absolute;left:268px;" id="data-update-btn" data-toggle="modal" data-target="#myModal">
									<i class="ace-icon fa fa-edit bigger-110 "></i> 批量更新教师角色
								</button>

								<!-- 同步学校不能修改 -->
								<?php if(empty($rmxSchool['school_from'])): ?>
									<button type="button" class="btn btn-danger btn-sm" style="height: 30px;position:absolute;left:440px;" id="del-teacher-batch-btn">
										<i class="ace-icon fa fa-times bigger-110 "></i> 批量删除教师
									</button>
								<?php endif ?>
							<?php endif ?>
						</div>
					</div>
				</div>
				<div class="form-inline keyword-search-div">
					<label class="keyword-search-label">绑定人</label>
					<div class="keyword-search-box" id="div-bind">
						<a href="javascript:;" class="keyword-search-keyword active" data-value="0">全部</a>
							<a href="javascript:;" class="keyword-search-keyword" data-value="1">有</a>
							<a href="javascript:;" class="keyword-search-keyword" data-value="2">无</a>
					</div>
				</div>
			</div>
		</div>
	</div>
	<!-- 学校表格 -->
	<table id="dynamic-table" class="table table-striped table-bordered table-hover" style="margin-top:10px">
		<thead>
			<tr>
				<th class="center">
					<label class="check-th">
						<input type="checkbox" class="ace" id="check-first">
						<span class="lbl"></span>
					</label>
				</th>
				<th>教师名称</th>
				<th>角色</th>
				<!-- <th>任课班级</th> -->
				<th>联系电话</th>
				<th>登录账号</th>
				<th>人脸图片</th>
				<th>绑定人</th>
				<th>创建时间</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>
<div id="myModal" class="modal fade">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header no-padding">
				<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button>
					批量更新选中的教师角色
				</div>
			</div>
			<div class="modal-body no-padding">
				<div class="row">
					<div class="col-xs-12">
						<form class="form-horizontal mt10" id="image-form">
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">所属角色：</label>
								<div class="col-sm-6">
								<div class="col-sm-12 tree">
									<ul id="role-tree"></ul>
								</div>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
			<div class="modal-footer no-margin-top" >
				<a href="javascript:;" class="btn btn-sm btn-primary" id="update-role">
					<i class="ace-icon fa fa-check"></i> 保存
				</a>
			</div>
		</div>
	</div>
</div>
<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<script type="text/javascript">
	var schoolId = 0;
	$(function(){
		var table = $('#dynamic-table').dt({
			"ajaxUrl": '<?=admin_url('school/get_teacher_list')?>',
			"columns": [
				{"data": function(ret){
					return "<div class='center'><label class='check-td'><input type='checkbox' class='ace check-list' value=" + ret.id + "><span class='lbl'></span></label></div>";
				}},	
				{"data": function(ret){
					if(ret.gender == '男') {
						var gender = ' <i class="fa fa-mars blue"></i>';
					} else {
						var gender = ' <i class="fa fa-venus red"></i>';
					}
					return ret.realname + gender;
	            }},
	            {"data": function(ret){
	            	var roleHtml = "";
	            	for(var i in ret.role) roleHtml += '<span class="label label-success mr10">'+ ret.role[i] +'</span>';
	            	return roleHtml;
				}},
	            // {"data": function(ret){
	            // 	var html = "";
	            // 	for(var i in ret.classList) html += '<span class="label label-success mr10">'+ ret.classList[i]['class_name'] +'</span>';
	            // 	return html;
	            // }},
	            {"data": "mobile"},
	            {"data": "username"},
				{"data": function(ret){
	            	return ret.face_image ? '<span class="label label-success">已录入</span>' : '';
	            }},
				{"data": "bindUsers"},
	            {"data": "createtime"},
	            {"data": function(ret){
					var action = '';
					// 教师权限不能编辑（可以编辑本人的）
					<?php if($rmxUser['user_type'] != USER_TYPE_TEACHER): ?>
						action += '<a class="mr20" target="_blank" href="<?=admin_url('school/teacher_edit')?>/'+ ret.id +'"><i class="fa fa-edit"></i> 修改</a>';
						action += '<a class="reset-password mr20" data-id="'+ ret.id +'" href="javascript:;"><i class="fa fa-asterisk"></i> 重置密码</a>';
						<?php if(! $rmxSchool['school_from']): ?>
							action += '<a class="text-danger delete-teacher" href="javascript:" data-id=' + ret.id +  ' data-title=' + ret.realname + '><i class="fa fa-times"></i> 删除</a>';
						<?php endif ?>
					<?php else: ?>
						// 本人登录
						if(<?= $rmxUser['id'] ?> == ret.id) {
							action += '<a class="mr20" href="<?=admin_url('school/teacher_edit')?>/'+ ret.id +'"><i class="fa fa-edit"></i> 修改</a>';
							action += '<a class="reset-password mr20" data-id="'+ ret.id +'" href="javascript:;"><i class="fa fa-asterisk"></i> 重置密码</a>';
							<?php if(! $rmxSchool['school_from']): ?>
								// 本人不能删除自己
								// action += '<a class="text-danger delete-teacher" href="javascript:" data-id=' + ret.id +  ' data-title=' + ret.realname + '><i class="fa fa-times"></i> 删除</a>';
							<?php endif ?>
						}
					<?php endif ?>
	                return action;
	            }},
			]
		});
		var roleTree = $("#role-tree");
		// 调用角色控件
		select_role(<?=$roleDatasource?>);
		// 重置登录密码
		$("body").on("click", ".reset-password", function(){
			var id = $(this).data("id");
			layer.confirm('确定将密码重置为“123456”吗？', function(index){
				layer.close(index);
				// 提交数据
				var actionUrl = "<?=admin_url('school/reset_teacher_password')?>";
				var actionData = {"id": id};
				ajax_get(actionUrl, actionData);
			});
		})
		// 点击关键字
		$("body").on("click", ".keyword-search-keyword", function(){
			var id = $(this).data("id");
			// 高亮当前点击对象
			$(this).parent().find(".keyword-search-keyword").removeClass("active");
			$(this).addClass("active");
			table.setAjaxData({
				"keyword": $("[name=keyword]").val(),
				"bind": $("#div-bind .active").data("value")
			});
		});
		// 内容筛选
		$("#data-search-btn").click(function(){
			table.setAjaxData({
				"keyword": $("[name=keyword]").val(),
				"bind": $("#div-bind .active").data("value")
			});
		});
		// 敲回车就搜索
		$("[name=keyword]").keypress(function (e) {
                if (e.which == 13) {
                    $("#btn-search").trigger("click");
                }
		});
		// 清空搜索框
		$("[name=keyword]").keyup(function() {
			if($(this).val().length > 0) {
				$("#empty-search").show();
				$("#empty-search").click(function(){
					$("[name=keyword]").val('');
					$(this).hide();
					// 重置搜索结果
					table.setAjaxData({
						"bind": $("#div-bind .active").data("value")
					});
				})
			} else {
				$("#empty-search").hide();
			}
		}); 
		// 搜索标题
		$("#btn-search").click(function() {
			var keyword = $("[name=keyword]").val();
			if(keyword != '') {
				table.setAjaxData({
					"keyword": keyword,
					"bind": $("#div-bind .active").data("value")
				})
			}
		})
		// 全选复选款
		$("#check-first").change(function(e) { 
			var checked =  $("#check-first").is(':checked'); 
			if(checked){
				$('.check-list').each(function (i){
					$(this).prop('checked', true);
				})
			} else{
				$('.check-list').each(function (i){
					$(this).prop('checked', false);
				})
			}
		});
		setTimeout(() => {
			$(".check-list").change(function(e) { 
				var checked = $(this).is(':checked');
				if(checked) {
					var allChecked = true;
					$('.check-list').each(function (i){
						if(!$(this).is(':checked')) {
							allChecked = false;
						}
					})
					if(allChecked) {
						$("#check-first").prop('checked', true);
					} else {
						$("#check-first").prop('checked', false);
					}
				} else {
					$("#check-first").prop('checked', false);
				}
			});
		}, 500);
		setTimeout(() => {
			$(".check-list").change(function(e) { 
				var checked = $(this).is(':checked');
				if(checked) {
					var allChecked = true;
					$('.check-list').each(function (i){
						if(!$(this).is(':checked')) {
							allChecked = false;
						}
					})
					if(allChecked) {
						$("#check-first").prop('checked', true);
					} else {
						$("#check-first").prop('checked', false);
					}
				} else {
					$("#check-first").prop('checked', false);
				}
			});
		}, 2000);
		// 批量更新教师权限
		$("#update-role").click(function(){
			var list = $("input:checkbox.check-list:checked");
			var lis =  $(".tree-selected");
			var idArr = new Array();
			var roleArr = new Array();
			list.each(function(){
				idArr.push($(this).val());
			});
			lis.each(function(){
				roleArr.push($(this).attr('id'));
			});
			var postUrl = "<?=admin_url('school/update_teacher_role')?>";
			var postData = {"id_arr": idArr, "role_arr": roleArr};
			ajax_post(postUrl, postData, function(res){
				window.location.reload();
			})
		});
		// 批量删除教师
		$("#del-teacher-batch-btn").click(function(){
			var list = $("input:checkbox.check-list:checked");
			var idArr = new Array();
			list.each(function(){
				idArr.push($(this).val());
			});
			var len = idArr.length;
			var postUrl = "<?=admin_url('school/delete_teacher_batch')?>";
			var postData = {"id_arr": idArr};
			if(len == 0) {
				ajax_post(postUrl, postData, function(res){
					window.location.reload();
				});
				return;
			}
			layer.confirm('确认批量删除教师？删除个数：' + len, { title:"提示", offset: '160px' }, function(){
				ajax_post(postUrl, postData, function(res){
					window.location.reload();
				})
			});

		});
		// 删除教师
		$("body").on('click', '.delete-teacher', function() {
			var id = $(this).data('id');
			var title = $(this).data('title');
			var posturl = "<?= admin_url("school/delete_teacher_action") ?>";
			var data = {
				'id': id
			};
			layer.confirm('确认删除：' + title + '？', { title:"提示", offset: '160px' }, function(){
				ajax_post(posturl, data, function() {
					// 刷新页面
					window.location.href = "<?= admin_url('school/teacher') ?>";
				});
			});
		});
		// 导入
		var upFromurl = '<?=UP_FORM_URL?>';
		var signature = '<?=$signature?>';
		var policy = '<?=$policy?>';
		$("#import").uploader({
			"server": upFromurl,
			"authorization": signature,
			"policy": policy,
			// 只支持上传 xls
			"accept": "xls",
			"callback": function(response) {
				// 上传后的文件名
				var fileLink = upyunUrl + response.url;
				ajax_post("<?= admin_url('import/import_teacher') ?>", { fileLink: fileLink}, function(res) {
					// 刷新页面
					location.reload();
				});
			}
		});
		// 设置选择文件按钮大小
		setTimeout(function(){
			$(".webuploader-pick").next().width(158).height(32);
		}, 100);

		// 导出数据
		$("#export_teacher").click(function() {
			var keyword = $("[name=keyword]").val();
			$url = "<?=admin_url('school/export_teacher?')?>";
			if(keyword) {
				$url += "keyword=" + keyword;
			}
			// console.log($params);return;
			window.location.href  = $url;
		});

		// 管理员才可解绑
		<?php if($rmxUser['user_type'] == USER_TYPE_SCHOOL): ?>
			// 解绑
			$("body").on('click', '.unbind', function() {
				var id = $(this).data('id');
				var nickname = $(this).text();
				var that = $(this);
				// alert(nickname);
				layer.confirm('确定解绑：' + nickname + '？', function(index){
					layer.close(index);
					// 提交数据
					var actionUrl = "<?=admin_url('school/unbind_action')?>";
					var actionData = {"id": id};
					ajax_get(actionUrl, actionData, function() {
						that.empty();
					});
				});
			});
		<?php endif ?>	
	})
</script>